import requests

from simpleutil.web import parse_curl
kb_id = 28

post_curl="""
curl 'https://datagrand.cccc-sdc.com/api/v1/knowledge_item/knowledge_details' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA2X3pzZ2w=' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MzgzNjYsIm5iZiI6MTczNDkzODM2NiwianRpIjoiMDE2NTM1YTMtMDk4ZC00MTc1LWE5MGItMmQxNDQ3MzI4NGU4IiwiZXhwIjoxNzM1NTQzMTY2LCJpZGVudGl0eSI6WyJhZG1pbiIsbnVsbF0sImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyIsInVzZXJfY2xhaW1zIjp7InVzZXJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImxldmVsIjoiXHU4ZDg1XHU3ZWE3XHU3YmExXHU3NDA2XHU1NDU4IiwidXNlcl9uaWNrbmFtZSI6Ilx1OGQ4NVx1N2VhN1x1N2JhMVx1NzQwNlx1NTQ1OCIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.iQ9pcPSRaQDeY9DEYo3UyJ2E2iAATrTQQe9d8IZrNI4' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryFBad7qcBArj25Adu' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: https://datagrand.cccc-sdc.com' \
  -H 'Referer: https://datagrand.cccc-sdc.com/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199606' \
  -H 'X-APP-NAME: zsgl' \
  -H 'X-SERVICE-TYPE: km' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --data-raw $'------WebKitFormBoundaryFBad7qcBArj25Adu\r\nContent-Disposition: form-data; name="kb_id"\r\n\r\n28\r\n------WebKitFormBoundaryFBad7qcBArj25Adu\r\nContent-Disposition: form-data; name="cate_parent_id"\r\n\r\n5850\r\n------WebKitFormBoundaryFBad7qcBArj25Adu\r\nContent-Disposition: form-data; name="dir_type"\r\n\r\n0\r\n------WebKitFormBoundaryFBad7qcBArj25Adu--\r\n' \
  --compressed
"""

put_curl="""
curl 'https://datagrand.cccc-sdc.com/api/v1/vpu/knowledge_item/knowledge_draft' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA2X3pzZ2w=' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MzYwMTcsIm5iZiI6MTczNDkzNjAxNywianRpIjoiMjFkNTM2NmItMTkyNC00YTg2LTliMGItYzRiY2Q4NzVkYzBiIiwiZXhwIjoxNzM1NTQwODE3LCJpZGVudGl0eSI6WyJhZG1pbiIsbnVsbF0sImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyIsInVzZXJfY2xhaW1zIjp7InVzZXJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImxldmVsIjoiXHU4ZDg1XHU3ZWE3XHU3YmExXHU3NDA2XHU1NDU4IiwidXNlcl9uaWNrbmFtZSI6Ilx1OGQ4NVx1N2VhN1x1N2JhMVx1NzQwNlx1NTQ1OCIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.rxy4pEBmrjzXnxd36myGDoHSEd-D6w8dNtwm686_QZ4' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: https://datagrand.cccc-sdc.com' \
  -H 'Referer: https://datagrand.cccc-sdc.com/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'Use-Progress: NO' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199606' \
  -H 'X-APP-NAME: zsgl' \
  -H 'X-SERVICE-TYPE: km' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --data-raw '{"knowledge_id":5930,"name":"xxxxx","content":"[{\"id\":\"JH9zV\",\"type\":\"p\",\"children\":[{\"text\":\"ssss\"}]}]","kb_id":28,"cate_id":5930,"tag_ids":""}' \
  --compressed
"""

approve_curl="""
curl 'https://datagrand.cccc-sdc.com/api/v1/knowledge_approve/approve' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA2X3pzZ2w=' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MzYwMTcsIm5iZiI6MTczNDkzNjAxNywianRpIjoiMjFkNTM2NmItMTkyNC00YTg2LTliMGItYzRiY2Q4NzVkYzBiIiwiZXhwIjoxNzM1NTQwODE3LCJpZGVudGl0eSI6WyJhZG1pbiIsbnVsbF0sImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyIsInVzZXJfY2xhaW1zIjp7InVzZXJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImxldmVsIjoiXHU4ZDg1XHU3ZWE3XHU3YmExXHU3NDA2XHU1NDU4IiwidXNlcl9uaWNrbmFtZSI6Ilx1OGQ4NVx1N2VhN1x1N2JhMVx1NzQwNlx1NTQ1OCIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.rxy4pEBmrjzXnxd36myGDoHSEd-D6w8dNtwm686_QZ4' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: https://datagrand.cccc-sdc.com' \
  -H 'Referer: https://datagrand.cccc-sdc.com/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199606' \
  -H 'X-APP-NAME: zsgl' \
  -H 'X-SERVICE-TYPE: km' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --data-raw '{"knowledge_id":5930}' \
  --compressed
"""

pass_approve_curl="""
curl 'https://datagrand.cccc-sdc.com/api/v1/vpu/knowledge_approve/approve' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA2X3pzZ2w=' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MzczNjMsIm5iZiI6MTczNDkzNzM2MywianRpIjoiZjdlNDFlZDUtYzBmNS00ZmFhLWExZGYtNDIwYWJiOWE2MThkIiwiZXhwIjoxNzM1NTQyMTYzLCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjE1MDcsInVzZXJuYW1lIjoieXVmYW5nIiwibGV2ZWwiOiJcdTY2NmVcdTkwMWFcdTc1MjhcdTYyMzciLCJ1c2VyX25pY2tuYW1lIjoiXHU0ZjU5XHU2NTNlIiwidXNlcl9pY29uIjoiLi9hc3NldHMvaW1hZ2VzL2F2YXRhci5zdmciLCJ1c2VyX2NlbnRlcl9pZCI6bnVsbH19.dKsNNG5W2GzVKkpgfrwlLcu8VmA0ZIclkfDFXhjO-rI' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: https://datagrand.cccc-sdc.com' \
  -H 'Referer: https://datagrand.cccc-sdc.com/admin/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199606' \
  -H 'X-APP-NAME: zsgl' \
  -H 'X-SERVICE-TYPE: km' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --data-raw '{"approve_id":9332,"approve_status":1,"deny_cause":""}' \
  --compressed
"""

approve_list_curl="""
curl 'https://datagrand.cccc-sdc.com/api/v1/knowledge_approve/approve_list?page=1&per_page=15&approve_status=0&is_personal=0' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA2X3pzZ2w=' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MzczNjMsIm5iZiI6MTczNDkzNzM2MywianRpIjoiZjdlNDFlZDUtYzBmNS00ZmFhLWExZGYtNDIwYWJiOWE2MThkIiwiZXhwIjoxNzM1NTQyMTYzLCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjE1MDcsInVzZXJuYW1lIjoieXVmYW5nIiwibGV2ZWwiOiJcdTY2NmVcdTkwMWFcdTc1MjhcdTYyMzciLCJ1c2VyX25pY2tuYW1lIjoiXHU0ZjU5XHU2NTNlIiwidXNlcl9pY29uIjoiLi9hc3NldHMvaW1hZ2VzL2F2YXRhci5zdmciLCJ1c2VyX2NlbnRlcl9pZCI6bnVsbH19.dKsNNG5W2GzVKkpgfrwlLcu8VmA0ZIclkfDFXhjO-rI' \
  -H 'Connection: keep-alive' \
  -H 'KMS-TAG: kms' \
  -H 'Referer: https://datagrand.cccc-sdc.com/admin/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199606' \
  -H 'X-APP-NAME: zsgl' \
  -H 'X-SERVICE-TYPE: km' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --compressed
"""

post_method,post_url,post_headers,post_data = parse_curl(
    post_curl,['Content-Type']
    );

put_method,put_url,put_headers,put_data = parse_curl(
    put_curl,[]
    );

approve_method,approve_url,approve_headers,approve_data = parse_curl(
    approve_curl,[]
    );

approve_list_method,approve_list_url,approve_list_headers,approve_list_data = parse_curl(
    approve_list_curl,[]
    );

pass_approve_method,pass_approve_url,pass_approve_headers,pass_approve_data = parse_curl(
    pass_approve_curl,[]
    );

def post_rich_content_knowledge_detail(cate_parent_id:int):
    response = requests.post(
        url=post_url,
        headers=post_headers,
        # timeout=10,
        data={
            'kb_id': str(kb_id),
            'cate_parent_id': str(cate_parent_id),
            'dir_type': '0'
        })
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            return response_data['data']['cate_id'], response_data['data'][
                'knowledge_id']
        else:
            print(f"Error: {response_data}")
            return None, None
    else:
        print(f"HTTP Error: {response.status_code}")
        return None, None


def put_rich_content_knowledge_detail(knowledge_id, name, content,
                                      cate_id):
    data = {
            "knowledge_id": int(knowledge_id),
            "name": name,
            "author": 'admin',
            "content": content,
            "kb_id": int(kb_id),
            "cate_id": int(cate_id),
            "tag_ids": ""
        }
    response = requests.put(
        url=put_url,
        headers=put_headers,
        timeout=10,
        json=data)
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print(f"Success: {name}")
            return True
        else:
            print(f"Error: {name}")
            print(f"Error: {response_data}")
            return False
    else:
        print(f"HTTP Error: {name}")
        print(f"HTTP Error: {response.status_code}")
        return False


def approve(knowledge_id):
    response = requests.post(
        url=approve_url,
        headers=approve_headers,
        timeout=10,
        json={"knowledge_id": knowledge_id})
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print("approve Success")
        else:
            print(f"Error: {response_data}")
    else:
        print(f"HTTP Error: {response.status_code}")

def approve_list()->list:
    response = requests.get(
        url=approve_list_url,
        headers=approve_list_headers,
        timeout=10,
        params={
            'page': 1,
            'per_page': 15,
            'approve_status': 0,
            'is_personal': 0
        }
        )
    target_approve_ids = []
    json_data = response.json()
    items = (json_data.get('data', {}))['items']
    if response.status_code == 200 or response.status_code == 201:
        target_approve_ids = [item['id'] for item in items if item['kb_id'] == 28]
    else:
        print(f"HTTP Error: {response.status_code}")
    return target_approve_ids

def pass_approve(approve_id):
    response = requests.post(
        url=pass_approve_url,
        headers=pass_approve_headers,
        timeout=10,
        json={
                "approve_id": approve_id,
                "approve_status": 1,
                "deny_cause": ""
            }
        )
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print("pass approve Success")
        else:
            print(f"Error: {response_data}")
    else:
        print(f"HTTP Error: {response.status_code}")
